using System;
using System.Linq;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.BO
{
    public class DMSortOrderPropMustBeOnRelatedClassRule : IBusinessObjectRule
    {
        private IHasOwnerClass _businessObject;
        private readonly SingleRelationship<DMSortOrder> _sortOrderRelationship;
        private readonly SingleRelationship<DMClass> _dmClassRelationship;

        public DMSortOrderPropMustBeOnRelatedClassRule(SingleRelationship<DMSortOrder> sortOrderRelationship,
                                                SingleRelationship<DMClass> dmClassRelationship)
        {
//            if (businessObject == null) throw new ArgumentNullException("businessObject");
            if (sortOrderRelationship == null) throw new ArgumentNullException("sortOrderRelationship");
            if (dmClassRelationship == null) throw new ArgumentNullException("dmClassRelationship");
//            _businessObject = businessObject;
            _sortOrderRelationship = sortOrderRelationship;
            _dmClassRelationship = dmClassRelationship;
        }

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _businessObject = (IHasOwnerClass) bo;
            DMSortOrder dmSortOrder = _sortOrderRelationship.GetRelatedObject();
            if (dmSortOrder == null) return true;
            DMClass dmClass = _dmClassRelationship.GetRelatedObject();
            if (dmClass == null) return true;
            BusinessObjectCollection<DMSortProperty> dmSortProperties = dmSortOrder.DMSortProperties;
            BusinessObjectCollection<DMProperty> allProperties = dmClass.AllProperties;
            return PropExists(dmSortProperties, allProperties);
        }

        private bool PropExists(BusinessObjectCollection<DMSortProperty> dmSortProperties, BusinessObjectCollection<DMProperty> allProperties)
        {
            foreach (DMSortProperty dmSortProperty in dmSortProperties)
            {
                string[] sortOrderFields = dmSortProperty.SortOrderPropertyName.Split('.');
                string sortOrderFieldProp = sortOrderFields[sortOrderFields.Count() - 1];
                if (sortOrderFields.Count() != 1) continue;
                bool propExists = allProperties.Exists(dmProperty => dmProperty.PropertyName == sortOrderFieldProp);
                if (!propExists) return false;
            }
            return true;
        }

        public string Name
        {
            get { return "DMSortOrderPropMustBeOnClass"; }
        }

        public string Message
        {
            get { return string.Format("The sort columns for Relationship '{0}' on class '{1}' must belong to the class.", _businessObject, _businessObject.OwnerClassName); }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Warning; }
        }
    }
}